﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;

namespace CarLoanSystem
{
    public partial class frmPayments : Form
    {
        private static PaymentsItem payItem;
        private static object tmpId;
        private static int emp_Id;
        public frmPayments()
        {
            InitializeComponent();
        }

        public frmPayments(int[] arrayId, int empId)
        {
            InitializeComponent();
            tmpId = arrayId;
            payItem = DAL.PaymentInfoDAL.LoadPayment(arrayId[0]);
            payItem.PAYMENTS_DATE = DateTime.Now;
            payItem.PAYMENTS_STATUS = "I";
            payItem.EMPLOYEE_ID = empId;
            emp_Id = empId;
            this.txtAmount.Text = (payItem.PAY_MONTH * arrayId.Length).ToString();
            this.txtDate.Text = DateTime.Now.ToShortDateString();
            this.txtLoanId.Text = payItem.LOAN_ID.ToString();
            this.txtName.Text = payItem.CUSTOMER_NAME;
            this.txtPerId.Text = payItem.CUSTOMER_PERSONAL_ID;
            this.txtLoop.Text = arrayId.Length.ToString();
        }

        public frmPayments(int id,int empId)
        {
            tmpId = null;
            InitializeComponent();
            payItem = DAL.PaymentInfoDAL.LoadPayment(id);
            payItem.PAYMENTS_DATE = DateTime.Now;
            payItem.PAYMENTS_STATUS = "I";
            payItem.EMPLOYEE_ID = empId;
            this.txtAmount.Text = payItem.PAY_MONTH.ToString();
            this.txtDate.Text = DateTime.Now.ToShortDateString();
            this.txtLoanId.Text = payItem.LOAN_ID.ToString();
            this.txtName.Text = payItem.CUSTOMER_NAME;
            this.txtPerId.Text = payItem.CUSTOMER_PERSONAL_ID;
            this.txtLoop.Text = "1";
        }

        private void btnPay_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("ยืนยันการชำระเงิน", "ชำระเงิน", MessageBoxButtons.YesNo,MessageBoxIcon.Information);
            if (dr == DialogResult.Yes)
            {
                LoanItem loanItem = DAL.LoanInfoDAL.LoadLoan(payItem.LOAN_ID);
                if (tmpId == null)
                {
                    DAL.PaymentInfoDAL.Update(payItem);
                    loanItem = DAL.LoanInfoDAL.LoadLoan(payItem.LOAN_ID);
                    loanItem.DEBT_MONTH_REST = loanItem.DEBT_MONTH_REST - payItem.PAY_MONTH;
                    DAL.LoanInfoDAL.UpdatePayMonth(loanItem);
                    MessageBox.Show("ชำระเงินสำเร็จ", "ชำระเงิน");
                    ShowReportSlip s = new ShowReportSlip(DAL.ReportInfoDAL.LoadSlip(payItem.PAYMENTS_ID));
                    s.ShowDialog();
                }
                else 
                {
                    int[] arrayId = (int[])tmpId;
                    for (int i = 0; i < arrayId.Length; i++) 
                    {
                        payItem = DAL.PaymentInfoDAL.LoadPayment(arrayId[i]);
                        payItem.PAYMENTS_DATE = DateTime.Now;
                        payItem.PAYMENTS_STATUS = "I";
                        payItem.EMPLOYEE_ID = emp_Id;
                        DAL.PaymentInfoDAL.Update(payItem);
                        loanItem = DAL.LoanInfoDAL.LoadLoan(payItem.LOAN_ID);
                        loanItem.DEBT_MONTH_REST = loanItem.DEBT_MONTH_REST - payItem.PAY_MONTH;
                        DAL.LoanInfoDAL.UpdatePayMonth(loanItem);
                    }
                    MessageBox.Show("ชำระเงินสำเร็จ", "ชำระเงิน");
                    ShowReportSlip s = new ShowReportSlip(DAL.ReportInfoDAL.LoadSlipAll(arrayId));
                    s.ShowDialog();
                }

                if (DAL.PaymentInfoDAL.CheckIsNotPayment(loanItem.LOAN_ID)) 
                {
                    DAL.CustomerDebtInfoDAL.Delete(loanItem.CUSTOMER_ID);
                    DAL.BondsmanInfoDAL.Delete(loanItem.CUSTOMER_ID);
                    DAL.BondsmanDocInfoDAL.DeleteAll(DAL.BondsmanInfoDAL.LoadBondsmanID(loanItem.CUSTOMER_ID));
                }
                this.Close();
            }
        }
    }
}
